from PyRTF import *

class Writer(object):
    '''
    A writer object is a class that describes a way to create RTF files
    
    It must implement two methods:
    - create_doc(*args) create the pyRTF doc
    - print_out() prints to the file


    
    '''
    def __init__(self, filename):
        self.filename = filename
        

    def create_doc(self, *args):
        pass

    def print_out(self):
        pass

class SimpleWriter(Writer):
    '''
    A simple writer that creates a RTF file with simple plain text and a single picture.
    '''
    def create_doc(self, title, text, picturefiles, save = True):
        self.doc     = Document()
    #    ss      = doc.StyleSheet
        self.section = Section()
        self.doc.Sections.append( self.section )

        self.section.append( r'{\b '+title+r'}' )
        print text.split('\n')
        for s in text.split('\n'):

            if s != '':
#                self.section.append( r'{\par '+s+r'}')
                self.section.append(Paragraph(s))

        
        if isinstance(picturefiles, str):
            picturefiles = [picturefiles]
        for picturefile in picturefiles:
            self.section.append( Paragraph( Image( picturefile, scale_x = 50, scale_y = 50 ) ) )
        if save:
            self.print_out()
        
    def print_out(self):
        DR = Renderer()
        f = file(self.filename, 'wb')
        DR.Write( self.doc, f)
        f.close()


